//
//  MyselfViewController.m
//  盘盈贷1.2
//
//  Created by iOS on 15/3/6.
//  Copyright (c) 2015年 iOS. All rights reserved.
//

#import "MyselfViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "APIRequest.h"
#import "ChiyouViewController.h"
#import "DongjieViewController.h"
#import "ShuhuiViewController.h"
#import "JiaoyiViewController.h"
#import "HongbaoViewController.h"
#import "AnquanViewController.h"
#import "YuViewController.h"
@interface MyselfViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *arr;
@property (nonatomic, retain)NSMutableArray *arr1;
@property (nonatomic, retain)NSMutableArray *arr2;
@property (nonatomic, retain)NSMutableArray *arr3;
@property (nonatomic, retain)UILabel *zhanghuyue;
@property (nonatomic, retain)NSDictionary *dic;
@property (nonatomic, retain)UILabel *bai;

@end

@implementation MyselfViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       
        self.navigationItem.hidesBackButton = YES;
        
        self.arr = [NSMutableArray arrayWithObjects:@"可用余额 (元)", @"", nil];
        self.arr1 = [NSMutableArray arrayWithObjects:@"交易记录", nil];
        self.arr2 = [NSMutableArray arrayWithObjects:@"我的红包", nil];
        self.arr3 = [NSMutableArray arrayWithObjects: @"账户安全", nil];
        
    
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    NSLog(@"________%@",self.strusername);

    self.navigationController.title = self.strusername;
    self.title = self.strusername;
    
    CGFloat width = self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height;
    
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, height / 11, width, height / 3.2)];
    topView.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:153 / 255.0 blue:0 /255.0 alpha:255];
    [self.view addSubview:topView];
    
    UILabel *shouyi = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 160, 30)];
    shouyi.textColor = [UIColor whiteColor];
    shouyi.text = @"昨日收益(元)";
    [topView addSubview:shouyi];
    [shouyi release];
    
    UILabel *shouyi1 = [[UILabel alloc] initWithFrame:CGRectMake(15, 60, 250, 40)];
    shouyi1.textColor = [UIColor whiteColor];
       shouyi1.font = [UIFont fontWithName:@"Helvetica" size:50];
    [topView addSubview:shouyi1];
    [shouyi1 release];
    [topView release];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, height / 3.2, self.view.frame.size.width, self.view.frame.size.height / 1.8) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.scrollEnabled = NO;
    [self.view addSubview:self.tableView];
    
    self.zhanghuyue = [[UILabel alloc] initWithFrame:CGRectMake(160, 34, 130, 20)];
    self.zhanghuyue.textColor = [UIColor lightGrayColor];
    self.zhanghuyue.textAlignment = NSTextAlignmentRight;
    [self.tableView addSubview:self.zhanghuyue];
    
    UILabel *chiyou = [[UILabel alloc] initWithFrame:CGRectMake(15, 110, 120, 20)];
    chiyou.textColor = [UIColor lightGrayColor];
    [self.tableView addSubview:chiyou];
    UILabel *dongjie = [[UILabel alloc] initWithFrame:CGRectMake(width / 1.8, 80, 120, 20)];
    dongjie.text = @"冻结金额(元)";
    [self.tableView addSubview:dongjie];
    [chiyou release];
    
    UILabel *chiyou1 = [[UILabel alloc] initWithFrame:CGRectMake(15, 80, 120, 20)];
    chiyou1.text = @"持有资产(元)";
    [self.tableView addSubview:chiyou1];
    UILabel *dongjie1 = [[UILabel alloc] initWithFrame:CGRectMake(width / 1.8, 110, 120, 20)];
    dongjie1.textColor = [UIColor lightGrayColor];
    
    [self.tableView addSubview:dongjie1];
    [dongjie1 release];
    [self.zhanghuyue release];
    [chiyou1 release];
    [self.tableView release];

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    button1.frame = CGRectMake(150, 120, 13, 13);
    button1.center = CGPointMake(150, 120);
    [button1 setImage:[UIImage imageNamed:@"iconfont-jiantouzhedie(1)"] forState:(UIControlStateNormal)];
   // button1.backgroundColor = [UIColor redColor];
    button1.tintColor = [UIColor lightGrayColor];
    [button1 addTarget:self action:@selector(chiyou:) forControlEvents:UIControlEventTouchUpInside];
    [self.tableView addSubview:button1];
    
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    button2.frame = CGRectMake(width / 1.12, 120, 13, 13);
    button2.center = CGPointMake(width / 1.12, 120);
    [button2 setImage:[UIImage imageNamed:@"iconfont-jiantouzhedie(1)"] forState:(UIControlStateNormal)];
    button2.tintColor = [UIColor lightGrayColor];
    // button1.backgroundColor = [UIColor redColor];
    [button2 addTarget:self action:@selector(dongjie:) forControlEvents:UIControlEventTouchUpInside];
    [self.tableView addSubview:button2];
    
    

    NSString *str = [NSString stringWithFormat:@"http://www.cloudpeng.com:8102/account/accountindex?strusername=%@", self.strusername];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if ([[responseObject  objectForKey:@"DayInterst"] intValue] > 1000) {
            NSMutableString *t1 = [NSMutableString stringWithFormat:@"%@", [responseObject  objectForKey:@"DayInterst"]];
            [t1 insertString:@"," atIndex:1];
            shouyi1.text = t1;
            NSMutableString *t2 =  [NSMutableString stringWithFormat:@"%@", [responseObject objectForKey:@"Available"]];
            [t2 insertString:@"," atIndex:3];
            self.zhanghuyue.text =t2;
            NSMutableString *t3 = [NSMutableString stringWithFormat:@"%@", [responseObject objectForKey:@"InvestAmount"]];
            [t3 insertString:@"," atIndex:5];
            [t3 insertString:@"," atIndex:2];
            chiyou.text = t3;
            NSMutableString *t4 = [NSMutableString stringWithFormat:@"%@.00", [responseObject objectForKey:@"Freeze"]];
            [t4 insertString:@"," atIndex:3];
            dongjie1.text = t4;
        }else{
//            NSMutableString *t1 = [NSMutableString stringWithFormat:@"%@", [responseObject  objectForKey:@"DayInterst"]];
//            [t1 insertString:@"," atIndex:1];
//            shouyi1.text = t1;
//            NSMutableString *t2 =  [NSMutableString stringWithFormat:@"%@", [responseObject objectForKey:@"Available"]];
//            [t2 insertString:@"," atIndex:3];
//            self.zhanghuyue.text =t2;
//            NSMutableString *t3 = [NSMutableString stringWithFormat:@"%@", [responseObject objectForKey:@"InvestAmount"]];
//            [t3 insertString:@"," atIndex:5];
//            [t3 insertString:@"," atIndex:2];
//            chiyou.text = t3;
//            NSMutableString *t4 = [NSMutableString stringWithFormat:@"%@.00", [responseObject objectForKey:@"Freeze"]];
//            [t4 insertString:@"," atIndex:3];
//            dongjie1.text = t4;
            shouyi1.text = [NSString stringWithFormat:@"%@", [responseObject  objectForKey:@"DayInterst"]];
            self.zhanghuyue.text = [NSString stringWithFormat:@"%@.00", [responseObject  objectForKey:@"Available"]];
            chiyou.text = [NSString stringWithFormat:@"%@", [responseObject  objectForKey:@"InvestAmount"]];
            dongjie1.text = [NSString stringWithFormat:@"%@.00", [responseObject  objectForKey:@"Freeze"]];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"~~~~~~~%@",error);
    }];
    

    
    NSString *str1 = [NSString stringWithFormat:@"http://www.cloudpeng.com:8102/audit/SecurityAudit?username=%@", self.strusername];
    
    AFHTTPRequestOperationManager *manager1 = [AFHTTPRequestOperationManager manager];
    //manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager1 GET:str1 parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject1) {
        
        self.dic = responseObject1;
        [self.tableView reloadData];
       
        int progresscount = 0;
        if ([[NSString stringWithFormat:@"%@", [self.dic objectForKey:@"IsEmailAudit"]]isEqual:@"1"]) {
            progresscount++;
        }
        if ([[NSString stringWithFormat:@"%@",[self.dic objectForKey:@"IsIdentityAudit"]]isEqual:@"1"]) {
            progresscount++;
        }
        if ([[NSString stringWithFormat:@"%@",[self.dic objectForKey:@"IsPhoneAudit"]]isEqual:@"1"]){
            progresscount++;
        }
        if ([[NSString stringWithFormat:@"%@",[self.dic objectForKey:@"HasAnswer"]]isEqual:@"1"]){
            progresscount++;
        }
        if ([[NSString stringWithFormat:@"%@",[self.dic objectForKey:@"HasBanks"]]isEqual:@"1"]){
            progresscount++;
        }
        if ([[NSString stringWithFormat:@"%@",[self.dic objectForKey:@"HasLocalPassword"]]isEqual:@"1"]){
            progresscount++;
        }
        if ([[NSString stringWithFormat:@"%@",[self.dic objectForKey:@"HasTradePwd"]]isEqual:@"1"]){
            progresscount++;
        }
        NSLog(@"progresscount:%d", progresscount);
        float a = progresscount;
        CGFloat result = a / 7;
        CGFloat result1 = (a / 7) * 100;
        [self.progress setProgress:result];
        NSLog(@"result:%f", result);
        self.bai.text = [NSString stringWithFormat:@"%.00f%%%", result1];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"~~~~~~~%@",error);
    }];
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(width / 3.55, height / 2.25, 190, 50)];
   //bottomView.backgroundColor = [UIColor yellowColor];
    self.progress = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
    self.progress.frame = CGRectMake(10, 25, 130, 60);
    //self.progress.center = CGPointMake(20, 10);
    self.progress.backgroundColor = [UIColor lightGrayColor];
    self.progress.tintColor = [UIColor redColor];
    [bottomView addSubview:self.progress];
    [self.tableView addSubview:bottomView];
    [self.progress release];
    
    self.bai = [[UILabel alloc] initWithFrame:CGRectMake(150, 10, 60, 30)];
    self.bai.textColor = [UIColor lightGrayColor];
    self.bai.font = [UIFont systemFontOfSize:15];
    [bottomView addSubview:self.bai];
    [self.bai release];
    
}
//设置tableview 每个分区(section)显示多少行(row)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    switch (section) {
        case 0:
            return self.arr.count;
            break;
        case 1:
            return self.arr1.count;
            break;
        case 2:
            return self.arr2.count;
            break;
        
            default:
            return self.arr3.count;
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        
        return 15.0;
    } else if (section == 4){
        return 20.0;
    } else {
        return 15.0;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{if ( indexPath.section == 0) {
    
    return 60;
    
    
} else {
    return 40.0;
}
    if (indexPath.row == 0) {
        return 40.0;
    }else {
        return 80.0;
    }
    
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"1"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"0"];
        }
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        cell.textLabel.text = self.arr[indexPath.row];
        
        return cell;
    } else if (indexPath.section == 1) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"1"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"1"];
        }
        cell.textLabel.text = self.arr1 [indexPath.row];
         cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
        
    } else if (indexPath.section == 2){
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"3"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"3"];
        }
        cell.textLabel.text = self.arr2 [indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    } else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"3"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"3"];
        }
        cell.textLabel.text = self.arr3 [indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}
- (void)chiyou:(id)sender
{
    ChiyouViewController *chiyou = [[ChiyouViewController alloc] init];
    chiyou.strusername =  self.strusername;
    UIBarButtonItem *backItem=[[UIBarButtonItem alloc]init];
    backItem.title=@"返回";
    //            backItem.tintColor=[UIColor colorWithRed:129/255.0 green:129/255.0  blue:129/255.0 alpha:1.0];
    self.navigationItem.backBarButtonItem = backItem;
    [backItem release];
    [self.navigationController pushViewController:chiyou animated:YES];
    [chiyou release];
}
- (void)dongjie:(id)sender
{
    DongjieViewController *dongjie = [[DongjieViewController alloc] init];
     dongjie.strusername =  self.strusername;
    UIBarButtonItem *backItem=[[UIBarButtonItem alloc]init];
    backItem.title=@"返回";
    //            backItem.tintColor=[UIColor colorWithRed:129/255.0 green:129/255.0  blue:129/255.0 alpha:1.0];
    self.navigationItem.backBarButtonItem = backItem;
    [backItem release];
    [self.navigationController pushViewController:dongjie animated:YES];
    [dongjie release];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            UIBarButtonItem *backItem=[[UIBarButtonItem alloc]init];
            backItem.title=@"返回";
//            backItem.tintColor=[UIColor colorWithRed:129/255.0 green:129/255.0  blue:129/255.0 alpha:1.0];
            self.navigationItem.backBarButtonItem = backItem;
            [backItem release];
            YuViewController *yu = [[YuViewController alloc] init];
            yu.strusername =  self.strusername;
            yu.yue = self.zhanghuyue.text;
            [self.navigationController pushViewController:yu animated:YES];
            self.zhanghuyue.text = yu.yue;
        }
    }
    if (indexPath.section == 1) {
//    if (indexPath.row == 1) {
//        ShuhuiViewController *Shuhui = [[ShuhuiViewController alloc] init];
//        [self.navigationController pushViewController:Shuhui animated:YES];
//    } else {
        
        JiaoyiViewController *jiaoyi = [[JiaoyiViewController alloc] init];
        jiaoyi.strusername =  self.strusername;
        UIBarButtonItem *backItem=[[UIBarButtonItem alloc]init];
        backItem.title=@"返回";
        //            backItem.tintColor=[UIColor colorWithRed:129/255.0 green:129/255.0  blue:129/255.0 alpha:1.0];
        self.navigationItem.backBarButtonItem = backItem;
        [backItem release];
        [self.navigationController pushViewController:jiaoyi animated:YES];
 //   }
    } else if (indexPath.section == 2) {
        HongbaoViewController *hongbao = [[HongbaoViewController alloc] init];
        hongbao.strusername =  self.strusername;
        UIBarButtonItem *backItem=[[UIBarButtonItem alloc]init];
        backItem.title=@"返回";
        //            backItem.tintColor=[UIColor colorWithRed:129/255.0 green:129/255.0  blue:129/255.0 alpha:1.0];
        self.navigationItem.backBarButtonItem = backItem;
        [backItem release];
        [self.navigationController pushViewController:hongbao animated:YES];
    } else {
        
    }
    if (indexPath.section == 3) {
        AnquanViewController *anquan = [[AnquanViewController alloc] init];
        UIBarButtonItem *backItem=[[UIBarButtonItem alloc]init];
        backItem.title=@"返回";
        //            backItem.tintColor=[UIColor colorWithRed:129/255.0 green:129/255.0  blue:129/255.0 alpha:1.0];
        self.navigationItem.backBarButtonItem = backItem;
        [backItem release];
        anquan.strname = self.strusername;
        [self.navigationController pushViewController:anquan animated:YES];
    }

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
